﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using ProjBackEnd.Areas.Sys.Models;
using ProjBackEnd.DBInstance;
using System.Reflection;

namespace ProjBackEnd.Areas.Sys.Controllers
{
    public class UserPermissionController : ApiController
    {
        /// <summary>
        /// 获取用户当前权限
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/sys/userpermission")]
        public RespUserPermission GetUserPermission(string key)
        {
            RespUserPermission resp = new RespUserPermission();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 3)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            List<VIEW_SYS_USERPERMISSION> lstPms = userLogic.GetUserPermission(strMemberid, strComcd);
            if (lstPms.Count == 0)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, strMemberid + ";" + strComcd);
                resp.statusCode = 60001;
                resp.statusMsg = "用户权限获取失败";
                return resp;
            }

            List<Menuinfo> lstMenuInfo = new List<Menuinfo>();
            foreach (VIEW_SYS_USERPERMISSION _collec in lstPms)
            {
                Menuinfo menuInfo = new Menuinfo();
                menuInfo.menuid = _collec.menuid;
                menuInfo.menucd = _collec.menucd;
                menuInfo.pmenuid = _collec.pmenuid;
                menuInfo.menunmchg = _collec.menunmchg;
                menuInfo.menunmeng = _collec.menunmeng;
                menuInfo.formnm = _collec.formnm;
                menuInfo.menuorder = _collec.menuorder;
                menuInfo.opentp = _collec.opentp;
                menuInfo.iconurl = _collec.iconurl;
                lstMenuInfo.Add(menuInfo);
            }

            resp.result = new ResultUserMenuInfo { menuinfo = lstMenuInfo };
            return resp;
        }

    }
}
